package org.ling.xuan.action.copy;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import org.jetbrains.annotations.NotNull;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.dao.ProjectDao;
import org.ling.xuan.entity.mo.ProjectSetting;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxOptional;
import org.ling.xuan.util.LxStrUtil;

import java.awt.*;
import java.awt.datatransfer.StringSelection;

/**
 * lx复制svn路径
 *
 * @author liting50684
 * @date 2023/08/21
 */
public class LxCopySvnPathAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String basePath = LxStrUtil.unionPath(LxOptional.of(e, c -> c.getProject().getBasePath()).orElse(""));
        String filePath = LxStrUtil.unionPath(LxOptional.of(e, c -> c.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE).getPath()).orElse(""));
        ProjectSetting projectSetting = ProjectDao.getSetting(basePath);
        String text = filePath.replace(projectSetting.getLocalPre(), projectSetting.getSvnPre());
        StringSelection selection = new StringSelection(text);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, selection);
        LoggerUtil.log(e.getProject(), "copy success:{}", text);
    }

    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().copyCondition(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
